#include "StdAfx.h"
#include "Reader.h"
#include "Algorithm.h"
#include "WinUnit.h"
#include "SGFile.h"
#include "Parse.h"
#include "RegExp.h"
#include "Jcop.h"

BEGIN_TEST(MyReaderTest)
{
	//CReaderLibApp reader;
	WIN_ASSERT_TRUE(Reader_InitInstance(),_T("READER address is NULL")); 
	unsigned char *name[2];
	WIN_ASSERT_TRUE(ListReaders(name)==2,_T("list READERs count error")); 

	WIN_ASSERT_TRUE(Open(),_T("READER open fail.")); 

	unsigned char input[261] = {0x00,0xA4,0x04,0x00,0x00};
	unsigned char resp[257];
	short respLen=0;
	//WIN_ASSERT_TRUE(APDU_T0(input,sw,1),_T("APDU filter open fail."));
	//WIN_ASSERT_TRUE(3 > 4);
	WIN_ASSERT_TRUE(APDU_T0(HD_handle,input,5,resp,&respLen,1)==0x9000,_T("APDU T0 fail."));

	
}
END_TEST

BEGIN_TEST(MyFilterReaderTest)
{
	//CReaderLibApp reader;
	Reader_InitInstance(); 

	WIN_ASSERT_TRUE(FilterLoadDll(),"ftloadDll:%x");

	WIN_ASSERT_TRUE(FilterOpen(),"ftopen:%hx"); 

	unsigned char input[260] ="00A4040008A00000000300000000";
	unsigned char sw[257];

	WIN_ASSERT_TRUE(Filter(input,sw,1)==-1,_T("APDU filter open fail."));
	
	
}
END_TEST
BEGIN_TEST(MyAlgoritmTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(Algorithm_InitInstance(),_T("Algorithm is NULL")); 
	unsigned char a[16] = {0x12,0xa6,0x3c,0x55,0x9f,0x20,0x83,0x6a,0x8d,0x65,0x80,0x00,0x00,0x00,0x00,0x00};
	unsigned char c[24] = 
		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0xa6,0x3c,0x55,0x9f,0x20,0x83,0x6a,0x8d,0x65,0x80,0x00,0x00,0x00,0x00,0x00};
	unsigned char b[16] = {0x87,0xef,0x4c,0x90,0x2d,0x99,0x0a,0x04,0x45,0x8F,0x21,0xAC,0x93,0xC6,0x00,0x6E};
	short srlen=8;
	unsigned char srMACHex[8];
	unsigned char srMACExp[8] = {0x5C,0x74,0x69,0x11,0x4B,0x52,0xD0,0x25};
	unsigned short bb = TDES_MACByte( c,24, b,16,srMACHex,&srlen );
	for(int i=0;i<8;i++)
		WIN_ASSERT_TRUE(srMACExp[i]==srMACHex[i],_T("TDES MAC Not correct!"));

	//int inputLen = 30;
 //  unsigned char input[30];
 //  unsigned char srSHAHex[20];
 //     unsigned char srSHAExp[20];
 //  int ret = SHA1_CompressByte(inputLen,input,srSHAHex);

	////WIN_ASSERT_TRUE(3 > 4);
	//for(int i=0;i<20;i++)
	//	WIN_ASSERT_TRUE(srSHAHex[i]==srSHAExp[i],_T("TDES SHA Not correct!"));
}
END_TEST
BEGIN_TEST(MyJCOPDLLTest)
{
	//CReaderLibApp reader;
	Reader_InitInstance(); 
	Dll_jo();
	Dll_co(8050);
	char data[100];
	short len=8;
	Dll_atr(data,&len);
	WIN_ASSERT_TRUE(18==len,"JCOP ATR");

	unsigned char input[260] ="$var=A00000000300000000";
		
	unsigned char sw[257];
	FilterTo(input,sw,0);
	
	unsigned char input1[260] = "00A4040008$var";
	FilterTo(input1,sw,0);
	WIN_ASSERT_TRUE(sw[0]=='6',_T("Jcop sw fail."));
	
}
END_TEST
BEGIN_TEST(MyTDESENCTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(Algorithm_InitInstance(),_T("Algorithm is NULL")); 
	unsigned char a[16] = {0x12,0xa6,0x3c,0x55,0x9f,0x20,0x83,0x6a,0x8d,0x65,0x80,0x00,0x00,0x00,0x00,0x00};
unsigned char b[16] = {0x87,0xef,0x4c,0x90,0x2d,0x99,0x0a,0x04,0x45,0x8F,0x21,0xAC,0x93,0xC6,0x00,0x6E};
unsigned char c[24] = 
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0xa6,0x3c,0x55,0x9f,0x20,0x83,0x6a,0x8d,0x65,0x80,0x00,0x00,0x00,0x00,0x00};
unsigned char d[8] = {0x87,0xEF,0x4C,0x90,0x2D,0x99,0x0A,0x04};
	unsigned char srHex[16];
	short srlen=16;
	unsigned char srExp[16] = 
{0x57,0x87,0x02,0x99,0x29,0x38,0x76,0x79,0x63,0x96,0x6B,0x05,0xF3,0x11,0x96,0x08};

	unsigned short bb = TDES_EncryptByte(a,16,b,16,0,srHex,&srlen);


	for(int i=0;i<16;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("TDES ENC Not correct!"));

}
END_TEST
BEGIN_TEST(MyCryptogramTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(Algorithm_InitInstance(),_T("Algorithm is NULL")); 
	unsigned char a[8] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
	unsigned char b[8] = {0x00,0x17,0xD5,0xE0,0xE4,0x35,0x45,0x92};
	unsigned char key[16] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F};
	unsigned char srHex[24];
	unsigned char srExp[8] = {0xB6,0x5A,0x6B,0xF6,0x17,0x3B,0xC1,0xE7};
	short cryptoLen=0;
	unsigned short bb = GetCryptogram(1,a,b,key,srHex,&cryptoLen);

	for(int i=0;i<8;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("Cryptogram Not correct!"));

}
END_TEST
BEGIN_TEST(MyMACTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(Algorithm_InitInstance(),_T("Algorithm is NULL")); 
	
	unsigned char apdu[13] = {0x84,0x82,0,0,0x10,0x00,0x17,0xD5,0xE0,0xE4,0x35,0x45,0x92};
	unsigned char cardCounter[8]={0x00,0x17,0xD5,0xE0,0xE4,0x35,0x45,0x92};
	unsigned char key[17] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0};
	//unsigned char svHex[8] =  {0x00,0x17,0xD5,0xE0,0xE4,0x35,0x45,0x92};
	unsigned char srHex[24];
	short srLen=0;
	//memcpy(apdu+5,svHex,8);
	GetMAC (apdu, 13,cardCounter,key,srHex,&srLen);
	unsigned char srExp[8]={0x1A,0x36,0x90,0x25,0x6B,0x5E,0xF4,0xB3};
	for(int i=0;i<8;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("MAC Not correct!"));

}
END_TEST
BEGIN_TEST(MySHA1Test)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(Algorithm_InitInstance(),_T("Algorithm is NULL")); 
	unsigned char a[16];
	memset(a,0x00,16);
	unsigned char srHex[20] ;
	unsigned char srExp[20] = 
	{0xE1,0x29,0xF2,0x7C,0x51,0x03,0xBC,0x5C,0xC4,0x4B,0xCD,0xF0,0xA1,0x5E,0x16,0x0D,0x44,0x50,0x66,0xFF};

	unsigned short bb = SHA1_CompressByte(16,a,srHex);


	for(int i=0;i<20;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("TDES SHA1 Not correct!"));

}
END_TEST

BEGIN_TEST(MyFILETest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(File_InitInstance(),_T("FileInstance is NULL")); 
	unsigned char a[2] ={'a','b'};
	char srHex[5];
	char srExp[] = {'6','1','6','2'};
	unsigned short bb = CstrToHstr(a, 2, 0, srHex);


	for(int i=0;i<4;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("TDES CharToHex Not correct!"));
	unsigned char b[3];
	bb = CharToByte(4,"6162",b);
	for(int i=0;i<2;i++)
		WIN_ASSERT_TRUE(a[i]==b[i],_T("TDES CharToHex Not correct!"));


}
END_TEST

BEGIN_TEST(MyBTCTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(File_InitInstance(),_T("FileInstance is NULL")); 
	unsigned char a[2] ={0x01,0x02};
	char srHex[5];
	char srExp[] = {'0','1','0','2'};
	unsigned short bb = CstrToHstr(a, 2, 0, srHex);


	for(int i=0;i<4;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("BTC Not correct!"));
	unsigned char b[3];
	bb = CharToByte(4,"0102",b);
	for(int i=0;i<2;i++)
		WIN_ASSERT_TRUE(a[i]==b[i],_T("CTB Not correct!"));


}
END_TEST
BEGIN_TEST(MyCovertTest)
{
	//Algorithm algo;
	WIN_ASSERT_TRUE(File_InitInstance(),_T("FileInstance is NULL")); 
	char srHex[3];
	char srExp[2] = {'7','F'};
	unsigned short bb = DstrHstr("127", 0, srHex);


	for(int i=0;i<2;i++)
		WIN_ASSERT_TRUE(srHex[i]==srExp[i],_T("DecToHex Not correct!"));

}
END_TEST
BEGIN_TEST(FileOpenTest)
{
	unsigned char buffer[11];
	WIN_ASSERT_TRUE(ReadCapFile(buffer)==4,"Read Fail or Empty");
}
END_TEST

//BEGIN_TEST(FileJcopTest)
//{  
//	Jcop jcop;
//	jcop.Connect(8050);
//	char recv[100];
//	short len=0;
//	jcop.ATR(recv,&len);
//	WIN_ASSERT_TRUE(len==18,"ATR len wrong"); 
//	char data1[9] = {0x00, 0x84,0x00, 0x00,0x08};
//	jcop.Send(data1,5);
//	jcop.Recv(recv,&len);
//	WIN_ASSERT_TRUE(len==2,"Get Challege should 6E00"); 
//}
//END_TEST
//BEGIN_TEST(FileJcopT0Test)
//{
//	Jcop jcop;
//	jcop.Connect(8050);
//	unsigned char recv[100];
//	short len=0;
//	jcop.ATR((char*)recv,&len);
//	
//	unsigned char data1[9] = {0x00, 0x84,0x00, 0x00,0x08};
//	short ret = jcop.APDU_T0(data1,5,recv,&len);
//	WIN_ASSERT_TRUE(ret==0x6e00,"Jcop");
//}
//END_TEST
BEGIN_TEST(FileTLVTest)
{
	WIN_ASSERT_TRUE(test_TLV()==0,"TLV fail");
}
END_TEST

BEGIN_TEST(CAPParseTest)
{
	WIN_ASSERT_TRUE(Test_CapAID()==0,"Cap Parse fail");
}
END_TEST

BEGIN_TEST(CAPParseAnotherTest)
{
	WIN_ASSERT_TRUE(Test_CapComponentTlv()==0,"Cap Parse Another fail");
}
END_TEST

		
BEGIN_TEST(FileREGEXTest)
{
	WIN_ASSERT_TRUE(isMatch(NULL,NULL),"TLV fail");
}
END_TEST


BEGIN_TEST(MySucceedingTest)
{
WIN_ASSERT_TRUE(4 > 3);
}
END_TEST
